import os
from pydub import AudioSegment
import speech_recognition as sr
from moviepy.editor import VideoFileClip
from flask import Flask, request, jsonify, render_template_string, send_file
import requests
import shutil
import time
import pandas as pd
import json

app = Flask(__name__)

def extract_audio_from_video(video_path, audio_path):
    video = VideoFileClip(video_path)
    video.audio.write_audiofile(audio_path)

def audio_to_text(audio_path):
    recognizer = sr.Recognizer()
    audio_file = sr.AudioFile(audio_path)

    with audio_file as source:
        audio_data = recognizer.record(source)

    try:
        text = recognizer.recognize_google(audio_data, language='ru-RU')
        return text
    except sr.UnknownValueError:
        return "Не удалось распознать речь"
    except sr.RequestError:
        return "Ошибка сервиса распознавания речи"

def process_video_for_audio_text(video_path, audio_path):
    start_time = time.time()
    extract_audio_from_video(video_path, audio_path)
    text = audio_to_text(audio_path)
    processing_time = time.time() - start_time
    return text, processing_time

def download_video(url, filename):
    with requests.get(url, stream=True) as r:
        with open(filename, 'wb') as f:
            shutil.copyfileobj(r.raw, f)

def safe_delete(file_path):
    """ Удаляет файл, если он существует """
    if os.path.exists(file_path):
        os.remove(file_path)

def save_results_to_json(results, filename):
    with open(filename, 'w', encoding='utf-8') as f:
        json.dump(results, f, ensure_ascii=False, indent=4)

@app.route('/')
def home():
    return render_template_string("""
        <!doctype html>
        <title>Привет, я Audio-to-Text сервис</title>
        <h1>Привет, я Audio-to-Text сервис</h1>
        <form action="/process_video" method="post" enctype="multipart/form-data">
          <input type="file" name="video">
          <input type="submit" value="Upload">
        </form>
        <br>
        <form action="/process_video_url" method="post">
          <input type="text" name="video_url" placeholder="Введите URL видео">
          <input type="submit" value="Download and Process">
        </form>
    """)

@app.route('/process_video', methods=['POST'])
def process_video_route():
    video_file = request.files['video']
    video_path = 'uploaded_video.mp4'
    audio_path = 'extracted_audio.wav'
    video_file.save(video_path)
    text, processing_time = process_video_for_audio_text(video_path, audio_path)
    # Безопасное удаление файлов после обработки
    safe_delete(video_path)
    safe_delete(audio_path)
    return jsonify({"text": text, "processing_time": processing_time})

@app.route('/process_video_url', methods=['POST'])
def process_video_url_route():
    if not request.is_json:
        return jsonify({"error": "Request must be JSON"}), 400

    data = request.get_json()
    video_url = data.get('video_url')

    if not video_url:
        return jsonify({"error": "No video_url provided"}), 400

    video_path = 'downloaded_video.mp4'
    audio_path = 'extracted_audio.wav'
    download_video(video_url, video_path)
    text, processing_time = process_video_for_audio_text(video_path, audio_path)
    # Безопасное удаление файлов после обработки
    safe_delete(video_path)
    safe_delete(audio_path)
    return jsonify({"text": text, "processing_time": processing_time})

@app.route('/process', methods=['POST'])
def process_csv_route():
    try:
        csv_path = 'yappy_hackaton_2024_400k.csv'
        
        df = pd.read_csv(csv_path)
        results = []
        out_filename = 'out3.json'

        for i, row in df.iterrows():
            video_url = row['video_url']
            video_path = f"video_{i}.mp4"
            audio_path = f"audio_{i}.wav"
            download_video(video_url, video_path)
            video_text, _ = process_video_for_audio_text(video_path, audio_path)
            results.append({"video_url": video_url, "text": video_text})
            safe_delete(video_path)
            safe_delete(audio_path)

            if (i + 1) % 50 == 0:
                save_results_to_json(results, out_filename)

        save_results_to_json(results, out_filename)
        safe_delete(csv_path)
        return jsonify({"message": "Processing complete, results saved to out.json"}), 200
    except Exception as e:
        app.logger.exception("An error occurred while processing the CSV file")
        return jsonify({"error": str(e)}), 500

@app.route('/download', methods=['GET'])
def download_json_route():
    try:
        json_path = 'out3.json'
        if os.path.exists(json_path):
            return send_file(json_path, as_attachment=True)
        else:
            return jsonify({"error": "File not found"}), 404
    except Exception as e:
        app.logger.exception("An error occurred while downloading the JSON file")
        return jsonify({"error": str(e)}), 500

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5002, debug=True)
